package com.example.vp2loadmoreview.banner;

import android.view.View;

import androidx.viewpager2.widget.ViewPager2;

public class CustomPageTransformer implements ViewPager2.PageTransformer {
    private float minScale = 0.85f;
    private float minAlpha = 0.5f;

    public void transformPage(View page, float position) {
        int pageWidth = page.getWidth();
        int pageHeight = page.getHeight();

        float scaleFactor = (1 - Math.abs(position)) < minScale ? minScale :
                (1 - Math.abs(position));
        float verticalMargin = pageHeight * (1 - scaleFactor) / 2;
        float horizontalMargin = pageWidth * (1 - scaleFactor) / 2;

        float translationX;
        if (position < 0) {
            translationX = horizontalMargin - verticalMargin / 2;
        }
        else {
            translationX = -horizontalMargin + verticalMargin / 2;
        }

        float alpha;
        page.setScaleX(scaleFactor);
        page.setScaleY(scaleFactor);

        alpha = minAlpha + (scaleFactor - minScale) / (1 - minScale) * (1 - minAlpha);

        page.setTranslationX(translationX);
        page.setAlpha(alpha);
    }

}
